﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class r_face_tree : System.Web.UI.Page
{
    private List<hedefgrup.reports.common.Tree> ExpandedTrees
    {
        get
        {
            return Session["ExpandedTrees"] == null ?
                (List<hedefgrup.reports.common.Tree>)(Session["ExpandedTrees"] = new List<hedefgrup.reports.common.Tree>()) :
                Session["ExpandedTrees"] as List<hedefgrup.reports.common.Tree>;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string toggleExpand = Request.QueryString["toggleexpand"];
        string viewDetails = Request.QueryString["viewdetails"];
        string appendTo = Request.QueryString["appendTo"];

        hedefgrup.reports.common.Tree selectedTree = null;
        hedefgrup.reports.common.Tree appendToTree = null;

        if (!string.IsNullOrEmpty(toggleExpand))
        {
            selectedTree = hedefgrup.reports.common.Tree.MemoryOnlyGetByUniqueID(toggleExpand);
        }

        if (!string.IsNullOrEmpty(viewDetails))
        {
            selectedTree = hedefgrup.reports.common.Tree.MemoryOnlyGetByUniqueID(viewDetails);
        }

        if (!string.IsNullOrEmpty(appendTo))
        {
            appendToTree = hedefgrup.reports.common.Tree.MemoryOnlyGetByUniqueID(appendTo);
        }

        if (IsPostBack)
        {
            if (Request.Form["save_command"] == "1")
            {
                if (appendToTree != null)
                {
                    selectedTree = appendToTree.CreateChild(Request.Form["tree_key"], Request.Form["tree_type"]);
                }

                if (selectedTree != null)
                {
                    selectedTree.Type = Request.Form["tree_type"];
                    selectedTree.Text = Request.Form["tree_text"];

                    SortedList<string, string> attributesToModify = new SortedList<string, string>();

                    string newAttributeKey = Request.Form["new_attribute_key"];
                    string newAttributeValue = Request.Form["new_attribute_value"];

                    if (!string.IsNullOrEmpty(newAttributeKey))
                    {
                        attributesToModify.Add(newAttributeKey, newAttributeValue);
                    }

                    foreach (hedefgrup.reports.common.Attribute attribute in selectedTree)
                    {
                        string attributeValue = Request.Form["attribute_value_" + attribute.Key];

                        if (attribute.Key == "Text")
                        {
                            attributeValue = Request.Form["tree_text"];
                        }

                        if (attribute.Value != attributeValue)
                        {
                            attributesToModify.Add(attribute.Key, attributeValue);
                        }
                    }

                    foreach (string attributeKey in attributesToModify.Keys)
                    {
                        selectedTree[attributeKey] = attributesToModify[attributeKey];
                    }
                }
            }
            else if (Request.Form["delete_command"] == "1")
            {
                // TODO: Implement..
            }
        }

        if (!string.IsNullOrEmpty(toggleExpand) && selectedTree != null)
        {
            List<hedefgrup.reports.common.Tree> expandedTrees = ExpandedTrees;
            if (expandedTrees.Contains(selectedTree))
            {
                expandedTrees.Remove(selectedTree);
            }
            else
            {
                expandedTrees.Add(selectedTree);
            }
        }

        if (selectedTree != null || appendToTree != null)
        {
            DisplayTreeDetails(selectedTree, appendToTree);
        }

        treeHtml.Text = hedefgrup.reports.common.Tree.Root.ToHtml(ExpandedTrees, selectedTree);
    }

    private void DisplayTreeDetails(hedefgrup.reports.common.Tree tree, hedefgrup.reports.common.Tree appendToTree)
    {
        StringBuilder htmlBuilder = new StringBuilder();
        if (tree == null || !tree.IsRoot)
        {
            htmlBuilder.AppendFormat("<li><table style=\"padding-left:{0}px;\">", 25);
            htmlBuilder.AppendFormat
            (
                "<thead>" +
                    "<tr>" +
                        "<th colspan=\"2\">&nbsp;</th>" +
                    "</tr>" +
                    "<tr>" +
                        "<th colspan=\"2\">{0}</th>" +
                    "</tr>" +
                    "<tr>" +
                        "<th colspan=\"2\">&nbsp;</th>" +
                    "</tr>" +
                "</thead>" +
                "<tbody>" +
                    "<tr>" +
                        "<td>Key</td><td><input class=\"attributevalue\" type='text' name='tree_key' value=\"{1}\" /></td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>Type</td><td><input class=\"attributevalue\" type='text' name='tree_type' value=\"{2}\" /></td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td>Text</td><td><input class=\"attributevalue\" type='text' name='tree_text' value=\"{3}\" /></td>" +
                    "</tr>",
                tree != null ? tree.Path : appendToTree.Path + "/[New Child]",
                tree != null ? tree.Key : "",
                tree != null ? tree.Type : "",
                tree != null ? tree.Text : ""
            );

            if (tree != null)
            {
                foreach (hedefgrup.reports.common.Attribute attribute in tree)
                {
                    if (attribute.Key != "Text")
                    {
                        htmlBuilder.AppendFormat("<tr><td>{0}:</td><td><input class=\"attributevalue\" type='text' name='attribute_value_{0}' value=\"{1}\" /></td></tr>", attribute.Key, attribute.Value);
                    }
                }
            }

            htmlBuilder.AppendFormat("<tr><td><input class=\"attributkey\" type='text' name='new_attribute_key' value=\"\" />:</td><td><input class=\"attributevalue\" type='text' name='new_attribute_value' value=\"\" /></td></tr>");

            htmlBuilder.AppendFormat
            (
                "<tr>" +
                    "<td colspan=\"2\"><input type=\"submit\" value=\"Save\" onclick=\"document.getElementById('save_command').value = '1';\"/><input type=\"hidden\" name=\"save_command\" id=\"save_command\" value=\"0\" /></td>" +
                    "<td colspan=\"2\"><input type=\"submit\" value=\"Delete\" onclick=\"document.getElementById('delete_command').value = '1';\"/><input type=\"hidden\" name=\"delete_command\" id=\"delete_command\" value=\"0\" /></td>" +
                "</tr>"
            );


            htmlBuilder.Append("</tbody></table></li>");
        }

        treeDetail.Text = htmlBuilder.ToString();
    }
}